package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName AdminController
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-08-21 16:36
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @Reference
    private AdminService adminService;

    @Reference
    private RoleService roleService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @PreAuthorize("hasAuthority('admin.show')")
    @RequestMapping
    public String toIndex(Model model, HttpServletRequest request){
        Map<String,Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return "admin/index";
    }


    @PreAuthorize("hasAuthority('admin.create')")
    @RequestMapping("/create")
    public String create(){
        return "admin/create";
    }

    @PreAuthorize("hasAuthority('admin.create')")
    @RequestMapping("/save")
    public String save(Admin admin){
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        admin.setHeadUrl("http://47.93.148.192:8080/group1/M00/03/F0/rBHu8mHqbpSAU0jVAAAgiJmKg0o148.jpg");
        adminService.insert(admin);
        return "common/successPage";
    }

    @PreAuthorize("hasAuthority('admin.edit')")
    @RequestMapping("/edit/{id}")
    public String edit(Model model, @PathVariable Long id){
        Admin admin = adminService.getById(id);
        model.addAttribute("admin",admin);
        return "admin/edit";
    }

    @PreAuthorize("hasAuthority('admin.edit')")
    @RequestMapping("/update")
    public String update(Admin admin){
        adminService.update(admin);
        return "common/successPage";
    }

    @PreAuthorize("hasAuthority('admin.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        adminService.delete(id);
        return "redirect:/admin";
    }

    //'/admin/uploadShow/'+id,
    @PreAuthorize("hasAuthority('admin.edit')")
    @RequestMapping("uploadShow/{id}")
    public String uploadShow(Model model,@PathVariable Long id){
        model.addAttribute("id",id);
        return "admin/upload";
    }
    // /admin/upload/{id}(id=${id}
    @PreAuthorize("hasAuthority('admin.edit')")
    @RequestMapping("upload/{id}")
    public String upload(@PathVariable Long id,MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString();
        fileName += originalFilename.substring(originalFilename.lastIndexOf("."));
        System.out.println("HouseImageController fileName="+fileName);
        //上传图片
        String url  = "http://rh7a83nvu.hb-bkt.clouddn.com/" + fileName;
        QiniuUtils.upload2Qiniu(file.getBytes(),fileName);

        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl(url);
        adminService.update(admin);
        return "common/successPage";
    }


    //'/admin/assignShow/'+id  进入分配角色页面
    @PreAuthorize("hasAuthority('admin.assign')")
    @RequestMapping("assignShow/{adminId}")
    public String assignShow(Model model,@PathVariable Long adminId){
        Map<String,Object> roleMap =  roleService.findRoleByAdminId(adminId);
        model.addAllAttributes(roleMap);
        model.addAttribute("adminId",adminId);
        return "admin/assignShow";
    }

    //@{/admin/assignRole}  根据用户分配角色
    @PreAuthorize("hasAuthority('admin.assign')")
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId,Long[] roleIds){
        roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }
}
